package com.yanqu.road.server.manager.union;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.impl.union.cross.CrossUnionActivityDaoImpl;
import com.yanqu.road.dao.impl.union.cross.CrossUnionUserActivityDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.entity.task.args.union.UnionActivityBaseArgs;
import com.yanqu.road.entity.task.args.union.UnionRechargeUserArgs;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.logic.pb.CrossActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.activity.AllActivityMgr;
import com.yanqu.road.server.manager.activity.CrossGeneralActivityRankMgr;
import com.yanqu.road.server.manager.activity.relation.CrossRelationRankMgr;
import com.yanqu.road.server.manager.orewar.CrossOreWarRankMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarRankMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class CrossUnionActivityDataMgr extends TempMgr {
    //活动id  conditionId  商会id
    private static Map<Integer, Map<Integer, Map<String, UnionActivityConditionData>>> unionActivityConditionDataMap = new ConcurrentHashMap<>();
    private static Map<Integer, Map<Integer, Map<Long, UserActivityConditionData>>> unionUserActivityDataMap = new ConcurrentHashMap<>();

    public static UnionActivityConditionData getUnionActivityConditionData(int activityId, int conditionType, long userId) {
        Map<Integer, Map<String, UnionActivityConditionData>> dataMap = unionActivityConditionDataMap.get(activityId);
        if (dataMap == null) {
            return null;
        }
        Map<String, UnionActivityConditionData> conditionDataMap = dataMap.get(conditionType);
        if (conditionDataMap == null) {
            return null;
        }
        for (UnionActivityConditionData data : new ArrayList<>(conditionDataMap.values())) {
            if (data.getMemberUserIdList().contains(userId)) {
                return data;
            }
        }
        return null;
    }

    public static List<UserActivityConditionData> getUserActivityConditionDataList(int activityId, int conditionType, List<Long> userLisr) {
        List<UserActivityConditionData> list = new ArrayList<>();
        Map<Long, UserActivityConditionData> conditionDataMap = getUserActivityConditionDataMap(activityId, conditionType);
        if (conditionDataMap == null) {
            return list;
        }
        for (Long userId : userLisr) {
            UserActivityConditionData data = conditionDataMap.get(userId);
            if (data != null) {
                list.add(data);
            }
        }
        return list;
    }

    public static void addUnionRankValue(int activityId, String unionUid, long longValue, UnionBaseInfo unionBaseInfo) {
        UnionActivityConditionData conditionData = getAndCreateUnionActivityConditionData(activityId, 0, unionUid, unionBaseInfo);
        conditionData.setExtraValue(BigInteger.valueOf(longValue));
        BigInteger unionValue = calcUnionValue(activityId, 0, unionUid, conditionData.getMemberUserIdList(), unionBaseInfo);
        conditionData.setValue(unionValue);
        unionActivityRandChange(activityId, conditionData, false, unionBaseInfo.getServerId());

    }

    public static void changeUnionRechargeData(int activityId, UnionRechargeUserArgs args, List<Long> serverList) {
        UnionActivityConditionData unionActivityConditionData = getAndCreateUnionActivityConditionData(activityId, 0, args.getUnionUid(), null);
        synchronized (unionActivityConditionData) {
            if (UnionActivityBaseArgs.VALUE_IN == args.getType()) {
                unionActivityConditionData.addMemberUserId(args.getUserId());
                unionActivityConditionData.setInfo(args.addInfo(unionActivityConditionData.getInfo()));
            } else if (UnionActivityBaseArgs.VALUE_OUT == args.getType()) {
                unionActivityConditionData.setInfo(args.removeInfo(unionActivityConditionData.getInfo()));
            }
            //同步给玩家
            ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
            if(null != activityInfo && null != serverList) {
                for(long serverId : serverList) {
                    if (activityInfo.getServerIdList().contains(serverId)) {
                        CrossUnionActivityRankDataProto.CrossUnionRechargeSyncMsg.Builder syncMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeSyncMsg.newBuilder();
                        syncMsg.setActivityId(activityId);
                        syncMsg.setUnionId(args.getUnionUid());
                        syncMsg.setInfo(unionActivityConditionData.getInfo());
                        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SYNC_CROSS_UNION_RECHARGE_INFO, syncMsg);
                        MessageHelper.sendPacket(serverId, 0, message);
                    }
                }
            }
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        List<Integer> allRunningActivityIds = AllActivityMgr.getAllRunningActivityIds();
        Map<Integer, Map<Long, Map<Integer, List<UserActivityConditionData>>>> allConditionDataMap = new CrossUnionUserActivityDaoImpl().getActivityAllConditionData(allRunningActivityIds);
        if (allConditionDataMap.size() > 0) {
            for (Map<Long, Map<Integer, List<UserActivityConditionData>>> entry : allConditionDataMap.values()) {
                for (Map<Integer, List<UserActivityConditionData>> listMap : entry.values()) {
                    for (List<UserActivityConditionData> dataList : listMap.values()) {
                        for (UserActivityConditionData data : dataList) {
                            int activityId = data.getActivityId();
                            int type = data.getType();
                            long userId = data.getUserId();
                            getAndCreateUserActivityConditionData(activityId, type, userId);
                            unionUserActivityDataMap.get(activityId).get(type).put(userId, data);
                        }
                    }
                }
            }
        }
        for (Integer activityId : allRunningActivityIds) {
            Map<Integer, Map<String, UnionActivityConditionData>> conditionDataMap = new CrossUnionActivityDaoImpl().getUnionActivityConditionDataMap(activityId);
            if (conditionDataMap.size() > 0) {
                unionActivityConditionDataMap.put(activityId, conditionDataMap);
            }
        }
        //     resetUnionValue();
        return true;
    }

  /*  private void resetUnionValue() {
        for (Integer activityId : unionUserActivityDataMap.keySet()) {
            Map<Integer, Map<Long, UserActivityConditionData>> map = unionUserActivityDataMap.get(activityId);
            for (Integer conditionType : map.keySet()) {
                for (CrossUnionInfo unionInfo : CrossUnionMgr.getUnionInfoMap().values()) {
                    if (unionInfo.isDelete()) {
                        continue;
                    }
                    ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
                    if (activityInfo.getServerIdList().contains(unionInfo.getServerId())) {
                        UnionActivityConditionData unionConditionData = getAndCreateUnionActivityConditionData(activityId, conditionType, unionInfo.getUnionUid());
                        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
                        List<Long> memberList = new ArrayList<>();
                        if (memberMap != null) {
                            memberList.addAll(memberMap.keySet());
                        }
                        BigInteger unionValue = calcUnionValue(activityId, conditionType, unionInfo.getUnionUid(), memberList);
                        if (!unionConditionData.getValue().equals(unionValue)) {
                            unionConditionData.setValue(unionValue);
                            unionConditionData.setLastUpdateTime(System.currentTimeMillis() / 1000);
                        }
                        resetUnionMember(unionConditionData);
                        unionActivityRandChange(activityId, unionConditionData, unionInfo.isDelete());
                    }
                }
            }
        }
    }*/

    @Override
    public boolean save() {
        removeOverTimeData();
        //   resetUnionValue();
        if (unionActivityConditionDataMap != null) {
            for (Map<Integer, Map<String, UnionActivityConditionData>> conditionMap : new ArrayList<>(unionActivityConditionDataMap.values())) {
                for (Map<String, UnionActivityConditionData> dataMap : new ArrayList<>(conditionMap.values())) {
                    for (UnionActivityConditionData data : new ArrayList<>(dataMap.values())) {
                        if (data.isInsertOption()) {
                            new CrossUnionActivityDaoImpl().add(data);
                        } else if (data.isUpdateOption()) {
                            new CrossUnionActivityDaoImpl().update(data);
                        }
                    }
                }
            }
        }
        if (unionUserActivityDataMap != null) {
            for (Map<Integer, Map<Long, UserActivityConditionData>> mapMap : new ArrayList<>(unionUserActivityDataMap.values())) {
                for (Map<Long, UserActivityConditionData> dataMap : new ArrayList<>(mapMap.values())) {
                    for (UserActivityConditionData data : new ArrayList<>(dataMap.values())) {
                        if (data.isInsertOption()) {
                            new CrossUnionUserActivityDaoImpl().add(data);
                        } else if (data.isUpdateOption()) {
                            new CrossUnionUserActivityDaoImpl().update(data);
                        }
                    }
                }
            }
        }
        return true;
    }

    private void removeOverTimeData() {
        long endTime = (System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2L)) / 1000;
        Set<Integer> needRemoveActivityIdList = new HashSet<>();
        Set<Integer> allActivityIds = new HashSet<>();

        allActivityIds.addAll(unionActivityConditionDataMap.keySet());
        allActivityIds.addAll(unionUserActivityDataMap.keySet());

        for (Integer activityId : allActivityIds) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                needRemoveActivityIdList.add(activityId);
                continue;
            }
            if (activityInfo.getEndShowTime() < endTime) {
                needRemoveActivityIdList.add(activityId);
            }
        }
        synchronized (unionActivityConditionDataMap) {
            for (Integer activityId : needRemoveActivityIdList) {
                unionActivityConditionDataMap.remove(activityId);
            }
        }
        synchronized (unionUserActivityDataMap) {
            for (Integer activityId : needRemoveActivityIdList) {
                unionUserActivityDataMap.remove(activityId);
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 改变玩家个人的值
     *  @param activityId 活动id
     * @param userId     玩家id
     * @param value      值
     * @param changeType
     * @param memberList
     */
    public static void changeUserData(int activityId, int conditionType, long userId, BigInteger value, String unionId, int changeType, List<CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg> memberList, long unionServerId, UnionBaseInfo unionBaseInfo) {
        if (userId > 0) {
            UserActivityConditionData conditionData = getAndCreateUserActivityConditionData(activityId, conditionType, userId);
            conditionData.setValue(value);
        }

        //如果有商会 就重新计算商会的值
        changeUnionValue(activityId, conditionType, unionId, changeType == UnionRiseRankArgs.NOTIFY_TYPE_DELETE, memberList, unionServerId, unionBaseInfo);

    }

    //只和商会有关的变动，和个人没有任何关系
    public static void changeUserData(int activityId, int conditionType, BigInteger value, String unionId, int changeType, Map<Long,Integer> memberMap, long unionServerId, UnionBaseInfo unionBaseInfo){
        changeUnionValue(activityId, conditionType, unionId, value,changeType == UnionRiseRankArgs.NOTIFY_TYPE_DELETE, memberMap, unionServerId, unionBaseInfo);
    }

    private static UserActivityConditionData getAndCreateUserActivityConditionData(int activityId, int conditionType, long userId) {
        if(!unionUserActivityDataMap.containsKey(activityId)){
            synchronized (unionUserActivityDataMap){
                if(!unionUserActivityDataMap.containsKey(activityId)){
                    unionUserActivityDataMap.put(activityId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Integer, Map<Long, UserActivityConditionData>> conditionMap = unionUserActivityDataMap.get(activityId);
        if(!conditionMap.containsKey(conditionType)){
            synchronized (conditionMap){
                if(!conditionMap.containsKey(conditionType)){
                    conditionMap.put(conditionType, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Long, UserActivityConditionData> userActivityConditionDataMap = conditionMap.get(conditionType);
        if(!userActivityConditionDataMap.containsKey(userId)){
            synchronized (userActivityConditionDataMap){
                if(!userActivityConditionDataMap.containsKey(userId)){
                    UserActivityConditionData data = new UserActivityConditionData();
                    data.setActivityId(activityId);
                    data.setUserId(userId);
                    data.setValue(BigInteger.ZERO);
                    data.setType(conditionType);
                    data.setActivityId(activityId);
                    data.setInsertOption();
                    userActivityConditionDataMap.put(userId, data);
                }
            }
        }
        return userActivityConditionDataMap.get(userId);
    }

    /**
     * 计算商会活动值
     *
     * @param activityId 活动id
     * @param unionId    商会id
     * @return
     */
    private static BigInteger calcUnionValue(int activityId, int conditionType, String unionId, List<Long> userList, UnionBaseInfo unionBaseInfo) {
        Map<Long, UserActivityConditionData> userActivityConditionDataMap = getUserActivityConditionDataMap(activityId, conditionType);
        if (userActivityConditionDataMap == null) {
            return BigInteger.ZERO;
        }
        BigInteger total = BigInteger.ZERO;
        for (Long userId : userList) {
            UserActivityConditionData data = userActivityConditionDataMap.get(userId);
            if (data != null) {
                BigInteger value = data.getValue();
                total = total.add(value);
            }
        }
        UnionActivityConditionData unionConditionData = getAndCreateUnionActivityConditionData(activityId, conditionType, unionId, unionBaseInfo);

        return total.add(unionConditionData.getExtraValue());
    }

    private static Map<Long, UserActivityConditionData> getUserActivityConditionDataMap(int activityId, int conditionType) {
        Map<Integer, Map<Long, UserActivityConditionData>> dataMap = unionUserActivityDataMap.get(activityId);
        if (dataMap == null) {
            return null;
        }
        return dataMap.get(conditionType);
    }

    private static void changeUnionValue(int activityId, int conditionType, String unionId, BigInteger value,boolean isUnionDel, Map<Long,Integer> memberMap, long unionServerId, UnionBaseInfo unionBaseInfo) {
        List<Long> memberList = new ArrayList<>();
        Map<Long, Integer> memberPositionMap = new HashMap<>();
        for (long userId : memberMap.keySet()) {
            memberList.add(userId);
            memberPositionMap.put(userId,memberMap.get(userId));
        }
        BigInteger unionValue = value;

        if (StringUtils.isNullOrEmpty(unionId)) {
            return;
        }
        UnionActivityConditionData unionConditionData = getAndCreateUnionActivityConditionData(activityId, conditionType, unionId, unionBaseInfo);
        if (!unionConditionData.getValue().equals(unionValue)) {
            unionConditionData.setValue(unionValue);
            unionConditionData.setLastUpdateTime(System.currentTimeMillis() / 1000);
        }

        unionConditionData.setMemberUserIdList(memberList);
        unionConditionData.setMemberPositionMap(memberPositionMap);
        if (isUnionDel) {
            unionConditionData.setMemberUserIdList(new ArrayList<>());
        }

        //触发排行
        unionActivityRandChange(activityId, unionConditionData, isUnionDel, unionServerId);
    }

    private static void changeUnionValue(int activityId, int conditionType, String unionId, boolean isUnionDel, List<CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg> memberPosDataMsgs, long unionServerId, UnionBaseInfo unionBaseInfo) {
        List<Long> memberList = new ArrayList<>();
        Map<Long, Integer> memberPositionMap = new HashMap<>();
        for (CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg msg : memberPosDataMsgs) {
            memberList.add(msg.getUserId());
            memberPositionMap.put(msg.getUserId(), msg.getPos());
        }
        BigInteger unionValue = calcUnionValue(activityId, conditionType, unionId, memberList, unionBaseInfo);

        if (StringUtils.isNullOrEmpty(unionId)) {
            return;
        }
        UnionActivityConditionData unionConditionData = getAndCreateUnionActivityConditionData(activityId, conditionType, unionId, unionBaseInfo);
        if (!unionConditionData.getValue().equals(unionValue)) {
            unionConditionData.setValue(unionValue);
            unionConditionData.setLastUpdateTime(System.currentTimeMillis() / 1000);
        }

        unionConditionData.setMemberUserIdList(memberList);
        unionConditionData.setMemberPositionMap(memberPositionMap);
        if (isUnionDel) {
            unionConditionData.setMemberUserIdList(new ArrayList<>());
        }

        //触发排行
        unionActivityRandChange(activityId, unionConditionData, isUnionDel, unionServerId);
    }

    public static void unionActivityRandChange(int activityId, UnionActivityConditionData unionConditionData, boolean isUnionDel, long unionServerId) {
        if (isCrossTradeActivity(activityId)) {
            //   C_CROSS_TRADE_WAR_UNION_RANK_CHANGE
            UnionActivityRank rank = new UnionActivityRank();
            rank.setUnionUid(unionConditionData.getUnionUid());
            rank.setValue(unionConditionData.getValue());
            rank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            rank.setActivityId(activityId);
            // 有并发问题 可以定时发送避免并发
            CrossTradeWarRankMgr.changeTradeWarUnionRank(activityId, rank);
        } else if (unionConditionData.getConditionType() == 0) {
            CrossUnionActivityRank rank = new CrossUnionActivityRank();
            rank.setUnionUid(unionConditionData.getUnionUid());
            rank.setValue(unionConditionData.getValue());
            rank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            rank.setActivityId(activityId);
            rank.setServerId(unionServerId);
            unionActivityRandChange(activityId, rank, isUnionDel);
        } else if (isStrongholdActivity(activityId)) {
            if (unionConditionData.getConditionType() == 90) {
                return;
            }
            CrossActivityUnionRank strongholdUnionRank = new CrossActivityUnionRank();
            strongholdUnionRank.setUnionUid(unionConditionData.getUnionUid());
            strongholdUnionRank.setValue(unionConditionData.getValue());
            strongholdUnionRank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            strongholdUnionRank.setActivityId(activityId);
            strongholdUnionRank.setServerId(unionServerId);
            strongholdUnionRankChange(activityId, unionConditionData.getConditionType(), strongholdUnionRank);
        } else if (isOreWarActivity(activityId)) {
            UnionActivityRank unionRank = new UnionActivityRank();
            unionRank.setUnionUid(unionConditionData.getUnionUid());
            unionRank.setValue(unionConditionData.getValue());
            unionRank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            unionRank.setActivityId(activityId);
            CrossOreWarRankMgr.changeOreWarUnionRank(activityId, unionRank, isUnionDel);
        } else if (isCookBoy(activityId)) {
            UnionActivityRank unionRank = new UnionActivityRank();
            unionRank.setUnionUid(unionConditionData.getUnionUid());
            unionRank.setValue(unionConditionData.getValue());
            unionRank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            unionRank.setActivityId(activityId);
            CrossRelationRankMgr.changeRelationUnionRank(activityId, unionRank, isUnionDel);
        } else {
            CrossActivityUnionRank generalUnionRank = new CrossActivityUnionRank();
            generalUnionRank.setUnionUid(unionConditionData.getUnionUid());
            generalUnionRank.setValue(unionConditionData.getValue());
            generalUnionRank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
            generalUnionRank.setActivityId(activityId);
            generalUnionRank.setServerId(unionServerId);
            generalActivityUnionRankChange(activityId, unionConditionData.getConditionType(), generalUnionRank, isUnionDel);
        }


    }

    public static boolean isCrossTradeActivity(int activityId) {
        ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return false;
        }
        return activityInfo.getType() == eActivityType.CrossTradeWarActivity.getValue();
    }

    private static boolean isCookBoy(int activityId) {
        ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return false;
        }
        return activityInfo.getType() == eActivityType.CookingBoyActivity.getValue();
    }

    private static boolean isOreWarActivity(int activityId) {
        ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return false;
        }
        return activityInfo.getType() == eActivityType.OreWarActivity.getValue();
    }

    private static boolean isStrongholdActivity(int activityId) {
        ActivityInfo activityInfo = AllActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return false;
        }
        return activityInfo.getType() == eActivityType.StrongholdWarActivity.getValue();
    }

    public static void unionActivityRandChange(int activityId, CrossUnionActivityRank activityRank, boolean isUnionDel) {
        // 有并发问题 可以定时发送避免并发

        ActivityRankMgr.changeActivityRank(activityId, activityRank, isUnionDel);
    }

    public static void generalActivityUnionRankChange(int activityId, int conditionType, CrossActivityUnionRank unionRank, boolean isDel) {
        // 大于0才允许上榜

        CrossGeneralActivityRankMgr.changeStrongholdUnionRank(activityId, conditionType, unionRank, isDel);
    }

    public static void strongholdUnionRankChange(int activityId, int conditionType, CrossActivityUnionRank unionRank) {
        if (unionRank.getValue().compareTo(BigInteger.ZERO) > 0) {
            // 大于0才允许上榜
            CrossStrongholdRankMgr.changeStrongholdUnionRank(activityId, conditionType, unionRank, false);
        }
    }

/*    private static void resetUnionMember(UnionActivityConditionData unionConditionData) {
        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionConditionData.getUnionUid());
        Map<Long, Integer> memberPositionMap = new HashMap<>();
        List<Long> memberList = new ArrayList<>();

        if (memberMap != null) {
            memberList.addAll(memberMap.keySet());
            for (CrossUnionMember member : memberMap.values()) {
                memberPositionMap.put(member.getUserId(), member.getPosition());
            }
        }
        unionConditionData.setMemberUserIdList(memberList);
        unionConditionData.setMemberPositionMap(memberPositionMap);
    }*/


    private static UnionActivityConditionData getAndCreateUnionActivityConditionData(int activityId, int conditionType, String unionId, UnionBaseInfo unionBaseInfo) {
        if(!unionActivityConditionDataMap.containsKey(activityId)){
            synchronized (unionActivityConditionDataMap){
                if(!unionActivityConditionDataMap.containsKey(activityId)){
                    unionActivityConditionDataMap.put(activityId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Integer, Map<String, UnionActivityConditionData>> dataMap = unionActivityConditionDataMap.get(activityId);
        if(!dataMap.containsKey(conditionType)){
            synchronized (dataMap){
                if(!dataMap.containsKey(conditionType)){
                    dataMap.put(conditionType, new ConcurrentHashMap<>());
                }
            }
        }
        Map<String, UnionActivityConditionData> conditionDataMap = dataMap.get(conditionType);
        if(!conditionDataMap.containsKey(unionId)){
            synchronized (conditionDataMap){
                if(!conditionDataMap.containsKey(unionId)){
                    UnionActivityConditionData unionConditionData = new UnionActivityConditionData();
                    unionConditionData.setActivityId(activityId);
                    unionConditionData.setGetReward(false);
                    unionConditionData.setUnionUid(unionId);
                    unionConditionData.setInfo("{}");
                    unionConditionData.setLastUpdateTime(System.currentTimeMillis() / 1000);
                    unionConditionData.setConditionType(conditionType);
                    unionConditionData.setInsertOption();
                    conditionDataMap.put(unionId, unionConditionData);
                }
            }
        }
        UnionActivityConditionData unionConditionData = conditionDataMap.get(unionId);
        if (unionBaseInfo != null) {
            String s = JSON.toJSONString(unionBaseInfo);
            unionConditionData.setUnionBaseInfo(unionBaseInfo);
            unionConditionData.setInfo(s);
        }
        return unionConditionData;
    }

    public static void rankChange(int activityId, int conditionType, String unionId, boolean isUnionDel) {
        Map<Integer, Map<String, UnionActivityConditionData>> dataMap = unionActivityConditionDataMap.get(activityId);
        if (dataMap == null) {
            return;
        }
        Map<String, UnionActivityConditionData> conditionDataMap = dataMap.get(conditionType);
        if (conditionDataMap == null) {
            return;
        }
        UnionActivityConditionData unionConditionData = conditionDataMap.get(unionId);
        if (unionConditionData == null) {
            return;
        }
        CrossUnionActivityRank activityRank = new CrossUnionActivityRank();
        activityRank.setUnionUid(unionConditionData.getUnionUid());
        activityRank.setValue(unionConditionData.getValue());
        activityRank.setUnionBaseInfo(unionConditionData.getUnionBaseInfo());
        activityRank.setActivityId(activityId);
        CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = CrossActivityPb.parseRankTempMsg(activityRank);
        CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.Builder rankMsg = CrossActivityProto.CrossActivityUnionRankChangeReqMsg2.newBuilder();
        rankMsg.setActivityId(activityId);
        rankMsg.addDataList(msg);
        rankMsg.setIsAll(false);
        rankMsg.setIsUnionDel(isUnionDel);
        YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_RANK_CHANGE, rankMsg);
        MessageHelper.sendPacket(1, 0, pbMessage);


       /* CrossActivityProto.CrossActivityUnionRankChangeReqMsg.Builder req = CrossActivityProto.CrossActivityUnionRankChangeReqMsg.newBuilder();
        req.setActivityId(activityId);
        req.setConditionType(conditionType);
        req.setIsUnionDel(isDel);
        req.setIsAll(false);
        if (unionRank != null) {
            UnionActivityRankProto.UnionActivityRankTempMsg.Builder msg = RankPb.parseUnionActivityRankTempMsg(unionRank);
            req.addDataList(msg);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GENERAL_ACTIVITY_UNION_RANK_CHANGE, req);
        GamePlayerMgr.sendChannelPacket(0, pbMsg);*/


    }

    /**
     * 获取商会活动玩家个人值
     *
     * @param activityId 活动id
     * @param unionId    商会id
     */
    public static void getUnionUserConditionData(int activityId, String unionId) {

    }

    public static UnionActivityConditionData getUnionActivityConditionData(int activityId, int conditionType, String unionId) {
        Map<Integer, Map<String, UnionActivityConditionData>> dataMap = unionActivityConditionDataMap.get(activityId);
        if (dataMap == null) {
            return null;
        }
        Map<String, UnionActivityConditionData> conditionDataMap = dataMap.get(conditionType);
        if (conditionDataMap == null) {
            return null;
        }
        return conditionDataMap.get(unionId);
    }

}
